from django.shortcuts import render,HttpResponse
from django.views.decorators.csrf import csrf_exempt
from app.vegetable_reptiles import vegetable_rep
from django.http import JsonResponse
from app.vegetable_reptiles import menu_video
from app.vegetable_reptiles import Home_page

'''
爬取首页数据
'''
@csrf_exempt
def getMenuIndex(request):
    if request.method == "POST":
        page = request.POST.get("page")
        try:
            # 爬取菜品菜谱列表信息
            result = Home_page.seach(page)
            data = {
                "errcode": 0,
                "errmsg": "成功",
                "data": {
                    "content": result
                }
            }
        except:
            data = {
                "errcode": 10000,
                "errmsg": "失败"
            }
    return JsonResponse(data=data)



'''
根据菜品名爬取菜谱列表
'''
@csrf_exempt
def seach_dish(request):
    if request.method == "POST":
        food_name = request.POST.get("food_name")
        page = request.POST.get("page")

        try:
            #爬取菜品菜谱列表信息
            result =  vegetable_rep.seach(food_name,page)
            data = {
                "errcode": 0,
                "errmsg": "成功",
                "data": {
                    "content": result
                }
            }
        except:
            data = {
                "errcode": 10000,
                "errmsg": "失败"
            }
    return JsonResponse(data=data)



'''
根据链接爬取菜品详细信息
'''
@csrf_exempt
def dish_desc(request):

    if request.method == "POST":
        food_href = request.POST.get("food_href")
        try:
            dict = {}
            dict["dish_href"] = food_href
            dict["food_desc_tile"]=vegetable_rep.food_desc_tile(food_href)
            dict["food_science_desc"] =vegetable_rep.food_science_desc(food_href)
            dict["food_operation"] =vegetable_rep.food_operation(food_href)
            data = {
                "errcode": 0,
                "errmsg": "成功",
                "data": {
                    "content": dict
                }
            }
        except:
            data = {
                "errcode": 10000,
                "errmsg": "失败"
            }

    return JsonResponse(data=data)


'''
菜谱分类爬取
'''
@csrf_exempt
def classification(request):
    if request.method == "POST":
        food_name = request.POST.get("food_name")
        page = request.POST.get("page")

        try:
            result = vegetable_rep.seach_dish_Byclassification(food_name,page)
            data = {
                "errcode": 0,
                "errmsg": "成功",
                "data": {
                    "content": result
                }
            }
        except:
            data = {
                "errcode": 10000,
                "errmsg": "失败"
            }
    return JsonResponse(data=data)


'''
获取菜谱视频
'''
@csrf_exempt
def getVideo(request):
    if request.method == "POST":
        page = request.POST.get('page')
        try:
            result = menu_video.video_list(page)
            data = {
                "errcode": 0,
                "errmsg": "成功",
                "data": {
                    "content": result
                }
            }
        except:
            data = {
                "errcode": 10000,
                "errmsg": "失败"
            }
    return JsonResponse(data=data)
